{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 02, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Hello From Quantum World </h2>\n",
    "\n",
    "This is our test file. \n",
    "\n",
    "Please run each cell one by one."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Sqaure brackets on the left sides of the code cells </h3>\n",
    "\n",
    "<ul>\n",
    "    <li> Each pair of the brackets shows the execution counter of the cell. </li>\n",
    "    <li> If the cell has not been executed, the inside of the brackets is empty.</li>\n",
    "    <li> When the code cell is still running, there appears <u>a single star</u> inside the brackets [*].</li>    \n",
    "    <li> When the execution is finished, there appears a number inside the brackets [number].</li>\n",
    "    <li> A cell can be executed many times. Each time it takes a new number (in increasing order). </li>\n",
    "</ul>\n",
    "\n",
    "The execution of a cell may take a few miliseconds, a few seconds, a few minutes, or much longer time.\n",
    "\n",
    "<font style=\"color:blue;\"> Therefore, you may not see any outcome until [*] is changed to [number].</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Python Test </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# I am a comment in python\n",
    "\n",
    "print(\"Hello From Quantum World :-)\")\n",
    "\n",
    "# please run me"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Quantum Circuit Test </h3>\n",
    "\n",
    "Our quantum programs are designed as quantum curcits. \n",
    "\n",
    "Explanations will appear in the main tutorial.\n",
    "\n",
    "<h4>We create a very simple quantum circuit</h4>\n",
    "\n",
    "Please run the cell below, and wait until the execution is completed.\n",
    "\n",
    "<i>Remark that we call a function or an object from a module in python as\n",
    "    \n",
    "<b>from</b> module-name <b>import</b> function-name\n",
    "    \n",
    "<b>from</b> module-name <b>import</b> object-name\n",
    "</i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from random import randrange\n",
    "\n",
    "# Create my circuit and register objects\n",
    "qreg = QuantumRegister(2) # my quantum register\n",
    "creg = ClassicalRegister(2) # my classical register \n",
    "circuit = QuantumCircuit(qreg,creg) # my quantum circuit\n",
    "\n",
    "# let's apply a Hadamard gate to the first qubit\n",
    "\n",
    "circuit.h(qreg[0])\n",
    "\n",
    "# let's set the second qubit to |1>\n",
    "circuit.x(qreg[1])\n",
    "\n",
    "# let's apply CNOT(first_qubit,second_qubit)\n",
    "circuit.cx(qreg[0],qreg[1])\n",
    "\n",
    "# let's measure the both qubits\n",
    "circuit.measure(qreg,creg)\n",
    "\n",
    "print(\"The execution was completed, and the circuit was created :)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>We execute the circuit (our program) 1024 times in the local simulator</h4>\n",
    "\n",
    "Each execution may have a different outcome (?).\n",
    "\n",
    "In our simple program, we expect to observe either 01 or 10 as the outcome in a single execution. \n",
    "\n",
    "We print the measurement results. \n",
    "\n",
    "Thus, we will see how many times '01' is observed and how many times '10' is observed.\n",
    "\n",
    "You may execute this cell a few times, and check the outcomes in each case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## execute the circuit 100 times\n",
    "job = execute(circuit,Aer.get_backend('qasm_simulator'),shots=1024)\n",
    "# get the result\n",
    "counts = job.result().get_counts(circuit)\n",
    "print(counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Draw the circuit</h4>\n",
    "\n",
    "We can draw our circuit.  \n",
    "\n",
    "If the circuit does not appear in the first run, please re-run the cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "# draw the overall circuit \n",
    "drawer(circuit)\n",
    "# re-execute me if you DO NOT see the circuit diagram"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>IBMQ Test</h3>\n",
    "\n",
    "<i> The remaining part requires <u>internet connection</u>.</i>\n",
    "\n",
    "We will execute our test circuit on IBM simulator, and then on one of IBM real qauntum computers.\n",
    "\n",
    "<font> Please wait the execution of each cell to be completed, before executing the next cell.</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Save your API on the disk </h4>\n",
    "\n",
    "Please write YOUR IBM API TOKEN in the following cell, and then run the cell.\n",
    "\n",
    "(The instruction is given at the end of section \"Testing: First Program\" in the file <a href=\"../bronze-start.html#IBMQ\">bronze-start.html</a>\n",
    "\n",
    "Once your YOUR IBM API TOKEN is saved on the disk,  it can be directly used later."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import IBMQ\n",
    "\n",
    "IBMQ.save_account('write YOUR IBM API TOKEN here')\n",
    "\n",
    "# Then, execute this cell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> See the stored account(s) </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IBMQ.stored_accounts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Load our account(s) </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IBMQ.load_accounts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> See the active account(s) </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IBMQ.active_accounts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>See available backends</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IBMQ.backends()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> See the currently operational real quantum computer(s)</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "IBMQ.backends(operational=True, simulator=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> See the least busy real quantum computer </h4> "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.backends.ibmq import least_busy\n",
    "least_busy(IBMQ.backends(simulator=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> IBMQ simulator </h3>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Use the simulator as backend </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "backend = least_busy(IBMQ.backends(simulator=True))\n",
    "\n",
    "backend.name()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Create a job for the backend </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import compile\n",
    "\n",
    "qobj = compile(circuit, backend=backend, shots=1024)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Execute this job on the simulator </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "job = backend.run(qobj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Check the result </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = job.result()\n",
    "counts = result.get_counts()\n",
    "print(counts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> IBMQ real quantum computers (Optional) </h3>\n",
    "\n",
    "Please read the following  part to see IBM's policy for usage for the prototype real quantum computers. [accessed on October 28, 2018]\n",
    "<br><br>\n",
    "<div style=\"background-color:#eeeeee;font-size:15px;\">\n",
    "    <b>Frequently Asked Questions</b>\n",
    "<br>\n",
    "<a href=\"https://quantumexperience.ng.bluemix.net/proxy/tutorial/full-user-guide/000-FAQ/000-Frequently_Asked_Questions.html\" target=\"_blank\">https://quantumexperience.ng.bluemix.net/proxy/tutorial/full-user-guide/000-FAQ/000-Frequently_Asked_Questions.html</a>\n",
    "\n",
    "How many experiments can I run?\n",
    "\n",
    "You can run as many experiments as you have Units to run; each experiment execution requires between 3 and 5 Units. No Units are required to perform simulations or to recall results of an experiment that was run previously.\n",
    "\n",
    "What happens when I run out of Units?\n",
    "\n",
    "You can still run simulations or recall the result of the experiments that have been run previously, but you must wait for the Units to replenish, which happens either once your execution has run off the queue, or 24 hours, whichever is greater.\n",
    "</div>\n",
    "\n",
    "<font color=\"blue\">You can check your <b>Units</b> in your <u>IBM Q Experience account</u> (My Account > Advanced).</font> "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Use the least busy real machine as backend </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "backend_real = least_busy(IBMQ.backends(simulator=False))\n",
    "\n",
    "backend_real.name()\n",
    "\n",
    "backend_real.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Execute the same job on a real machine </h4>\n",
    "\n",
    "<font style=\"color:red\">Depending on the number of pending jobs, it might take for a while to execute our job on the real machine.</font>\n",
    "\n",
    "<u> If you do not have enough Units, you should wait for 24 hours to execute your quantum program on a real machine. </u>\n",
    "\n",
    "<i> But, this would not be a problem to complete Bronze, because we use the local simulator for our tasks. </i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qobj_real = compile(circuit, backend=backend_real, shots=1024)\n",
    "\n",
    "job_real = backend_real.run(qobj_real)\n",
    "\n",
    "job_real.queue_position()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4> Check the result </h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_real = job_real.result()\n",
    "counts_real = result_real.get_counts()\n",
    "print(counts_real)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font style=\"color:blue\"> You may finish this notebook before getting the outcomes, if it seems to take long time.</font>\n",
    "\n",
    "You may observe more than two outcomes, different than the simulators. \n",
    "\n",
    "The expected outcomes '01' and '10' can still be observed more frequently, but we may also observe the unexpected outcomes '00' and '11'. Because, even after a single operation, the prototype quantum computers introduce some errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
